package com.express.e.root.controller;

import com.express.e.root.annotation.ResponseBody;
import com.express.e.root.bean.Message;
import com.express.e.root.bean.User;
import com.express.e.root.service.UserService;
import com.express.e.root.service.impl.UserServiceImpl;
import com.express.e.root.utils.FomatDateUtil;
import com.express.e.root.utils.JSONUtil;
import com.express.e.root.utils.UserWrap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserController {
    private UserService userService = new UserServiceImpl();
    @ResponseBody("/user/add.do")
    public String addUser(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        String pwd = request.getParameter("pwd");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String cardId = request.getParameter("cardId");
        User user = userService.addUser(
                new User(
                        name,
                        phone,
                        pwd,
                        cardId,
                        new Date(System.currentTimeMillis()),
                        new Date(System.currentTimeMillis())
                ));
        if (null == user || user.getUserName().isEmpty()) {
            return JSONUtil.toJson(new Message(400, "增加失败", null));
        }
        UserWrap userWrap = new UserWrap(
                user.getUserName(),
                user.getUserPhone(),
                user.getUserPwd(),
                user.getCardId(),
                FomatDateUtil.formatDate(user.getUserRegisterTime()),
                FomatDateUtil.formatDate(user.getUserLastLoginTime())
        );
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(userWrap);
        String insert = JSONUtil.toJson(new Message(200, "新增成功", objects));
        System.out.println(insert);
        return insert;
    }

    @ResponseBody("/user/delete.do")
    public String deleteUser(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("userId");
        Boolean delFlag = userService.delUser(Integer.valueOf(id));
        if (delFlag) {
            String toJson = JSONUtil.toJson(new Message(200, "删除成功", null));
            System.out.println(toJson);
            return toJson;
        }
        String toJson = JSONUtil.toJson(new Message(400, "删除失败", null));
        System.out.println(toJson);
        return toJson;
    }

    @ResponseBody("/user/findByCardId.do")
    public String findByCardId(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        String cardId = req.getParameter("cardId");
        System.out.println(cardId);
        if (cardId.isEmpty()) {
            return JSONUtil.toJson(new Message(401, "身份号码为空！", null));
        }
        User user = userService.queryByCardId(cardId);
        if (null != user  && user.getUserName()!=null && !user.getUserName().isEmpty()) {
            System.out.println(user.toString());
            UserWrap userWrap = new UserWrap(
                    user.getUserId(),
                    user.getUserName(),
                    user.getUserPhone(),
                    user.getUserPwd(),
                    user.getCardId(),
                    FomatDateUtil.formatDate(user.getUserRegisterTime()),
                    FomatDateUtil.formatDate(user.getUserLastLoginTime()),
                    user.getIsSaveWeekPwd()
            );
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(userWrap);
            String toJson = JSONUtil.toJson(new Message(200, "成功查寻到此用户！", objects));
            System.out.println(toJson);
            return toJson;
        }
        String toJson = JSONUtil.toJson(new Message(400, "未查到此用户！", null));
        System.out.println(toJson);
        return toJson;
    }

    @ResponseBody("/user/findByUserName.do")
    public String findByUserName(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        String userName = req.getParameter("userName");
        System.out.println(userName);
        if (null == userName||userName.isEmpty()) {
            return JSONUtil.toJson(new Message(401, "身份号码为空！", null));
        }
        User user = userService.queryByUserName(userName);
        if (null != user   && user.getUserName()!=null &&!user.getUserName().isEmpty()) {
            System.out.println(user.toString());
            UserWrap userWrap = new UserWrap(
                    user.getUserId(),
                    user.getUserName(),
                    user.getUserPhone(),
                    user.getUserPwd(),
                    user.getCardId(),
                    FomatDateUtil.formatDate(user.getUserRegisterTime()),
                    FomatDateUtil.formatDate(user.getUserLastLoginTime()),
                    user.getIsSaveWeekPwd()
            );
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(userWrap);
            String toJson = JSONUtil.toJson(new Message(200, "成功查寻到此用户名！", objects));
            System.out.println(toJson);
            return toJson;
        }
        String toJson = JSONUtil.toJson(new Message(400, "未查到此用户！", null));
        System.out.println(toJson);
        return toJson;
    }

    @ResponseBody("/user/update.do")
    public String updateUser(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        String temp = request.getParameter("userId");
        Integer userId = -1;
        if (temp != null) {
            userId = Integer.valueOf(temp);
        }
        System.out.println(userId);
        String userName = request.getParameter("userName");
        String phone = request.getParameter("phone");
        String cardId = request.getParameter("cardId");
        String userPwd = request.getParameter("userPwd");
        System.out.println(userName);
        System.out.println(phone);
        System.out.println(cardId);
        System.out.println(userPwd);
        User user = userService.queryById(userId);
        System.out.println(user);
        if (null != user) {
            User userUpdate = new User(
                    userId,
                    userName,
                    phone,
                    userPwd,
                    cardId,
                    user.getUserRegisterTime(),
                    user.getUserLastLoginTime(),
                    user.getIsSaveWeekPwd()
            );
            User userResult = userService.updateUser(userUpdate);
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(userResult);
            String toJson = JSONUtil.toJson(new Message(200, "修改成功", objects));
            System.out.println(toJson);
            return toJson;
        }
        return JSONUtil.toJson(new Message(400, "修改失败", null));
    }

    @ResponseBody("/user/findByPhone.do")
    public String findByPhone(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        String phone = req.getParameter("phone");
        System.out.println(phone);
        if (phone.isEmpty()) {
            return JSONUtil.toJson(new Message(401, "手机号码为空！", null));
        }
        User user = userService.queryByPhone(phone);

        if (null != user  && user.getUserName()!=null  && !user.getUserName().isEmpty()) {
            System.out.println(user.toString());
            UserWrap userWrap = new UserWrap(
                    user.getUserId(),
                    user.getUserName(),
                    user.getUserPhone(),
                    user.getUserPwd(),
                    user.getCardId(),
                    FomatDateUtil.formatDate(user.getUserRegisterTime()),
                    FomatDateUtil.formatDate(user.getUserLastLoginTime()),
                    user.getIsSaveWeekPwd()
            );
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(userWrap);
            String toJson = JSONUtil.toJson(new Message(200, "成功查寻到此用户手机！", objects));
            System.out.println(toJson);
            return toJson;
        }
        String toJson = JSONUtil.toJson(new Message(400, "未查到此用户！", null));
        System.out.println(toJson);
        return toJson;
    }

    @ResponseBody("/user/list.do")
    public String listUser(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        List<User> users = userService.userList();
        List usersNew = new ArrayList<>();
        for (User u :
                users) {
            UserWrap usersWrap = new UserWrap();
            usersWrap.setUserId(u.getUserId());
            usersWrap.setUserName(u.getUserName());
            usersWrap.setUserPhone(u.getUserPhone());
            usersWrap.setUserPwd(u.getUserPwd());
            usersWrap.setCardId(u.getCardId());
            usersWrap.setUserRegisterTime(FomatDateUtil.formatDate(u.getUserRegisterTime()));
            usersWrap.setUserLastLoginTime(FomatDateUtil.formatDate(u.getUserLastLoginTime()));
            usersNew.add(usersWrap);
        }
        if (null != usersNew) {
            String toJson = JSONUtil.toJson(new Message(200, "查找用户成功！", usersNew));
            System.out.println(toJson);
            return toJson;
        } else {
            String toJson = JSONUtil.toJson(new Message(400, "查找用户失败！", usersNew));
            System.out.println(toJson);
            return toJson;
        }
    }



}
